package com.zhxy.util.service.captcha;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

import com.zhxy.util.api.captcha.Captcha;
import com.zhxy.util.api.captcha.CaptchaBuilder;

import nl.captcha.backgrounds.GradiatedBackgroundProducer;
import nl.captcha.text.renderer.DefaultWordRenderer;

public class SimpleCaptchaBuilder implements CaptchaBuilder {
	
	private List<Color> colors;
	
	private List<Font> fonts;
	
	public SimpleCaptchaBuilder() {
		
		colors = new ArrayList<Color>();
		colors.add(Color.red);
		colors.add(Color.blue);
		colors.add(Color.yellow);
		colors.add(Color.black);
		
		fonts = new ArrayList<Font>();
		fonts.add(new Font("Arial", Font.BOLD, 48));
	}

	@Override
	public Captcha build() {
		
		DefaultWordRenderer word = new DefaultWordRenderer(colors, fonts); 
		nl.captcha.Captcha captcha = new nl.captcha.Captcha.Builder(150, 60).addText(word)
                .gimp()
                .addBorder()
                .addNoise()
                .addBackground(new GradiatedBackgroundProducer(Color.gray, Color.white))
                .build();
        
        DefaultCaptcha captcha2 = new DefaultCaptcha();
        captcha2.setAnswer(captcha.getAnswer());
        captcha2.setImage(captcha.getImage());
		return captcha2;
	}

}
